+#!/bin/sh -eu
+# SYNTAX: $database_pattern -- $snapshot_eval
+# DESCRIPTION: crée un instantané pour chaque base de données MySQL $database donnée
+# DESCRIPTION: dans ~backup/snapshot/mysql/"$database",
+# DESCRIPTION: puis évalue $snapshot_eval,
+# DESCRIPTION: puis efface l'instantané.
+${TRACE:+set -x}
+
+test "$(id -u)" = "$(id -u backup)"
+
+database_pattern=$1; shift
+test ! "${1-}" = -- || shift
+test ! $# = 0 || set -- :
+
+install -d -m 770 -o backup -g backup \
+ ~backup/snapshot/mysql
+
+snapshot () {
+ local database="$1"; shift
+ test ! -e ~backup/snapshot/mysql/$database
+ # NOTE: soit une sauvegarde est déjà en cours,
+ # soit une précédente a échoué.
+ can_hotcopy=yes
+ while IFS=' ' read -r table engine
+ do
+ case $engine in
+ (MyISAM|ARCHIVE) :;;
+ (*) can_hotcopy=no;;
+ esac
+ done <<-EOF
+ $(mysql -u backup --batch --skip-column-names <<-EOF
+ SELECT table_name,engine
+ FROM information_schema.tables
+ WHERE table_schema = '$database';
+ EOF
+ )
+ EOF
+ case $can_hotcopy in
+ (yes)
+ cd /
+ sudo -u root find ~mysql-data -mindepth 1 -maxdepth 1 -type d -exec \
+ install -d -m 750 -o mysql -g backup {} +
+ cd - >/dev/null 2>&1
+ test "${TRACE:+set}" || quiet=--quiet
+ mysqlhotcopy --method=cp -u backup ${quiet:-} \
+ "$database" ~backup/snapshot/mysql
+ ;;
+ (no)
+ install -d -m 770 -o backup -g backup \
+ ~backup/snapshot/mysql/"$database"
+ mysqldump --opt --events -u backup ${TRACE:+--verbose} \
+ --result-file ~backup/snapshot/mysql/$database/dump.sql \
+ "$database"
+ ;;
+ esac
+ eval "$@"
+ rm -rf \
+ ~backup/snapshot/mysql/"$database"
+ }
+
+exit=0
+while IFS=' ' read -r database
+ do if ! snapshot "$database" "$@"
+ then
+ exit=1
+ cat >&2 <<-EOF
+ ERROR: snapshot "$database" "$@"
+ EOF
+ fi
+ done <<-EOF
+ $(mysql -u backup --batch --skip-column-names <<-EOF
+ SELECT schema_name
+ FROM information_schema.schemata
+ WHERE schema_name NOT IN ('information_schema', 'performance_schema')
+ AND schema_name LIKE '$database_pattern';
+ EOF
+ )
+ EOF
+exit $exit